require 'test/unit'
require 'facets/core/kernel/with'

require 'models/mud_base'
require 'models/account'
require 'models/character'

class AccountTest < Test::Unit::TestCase

  def setup
    @account = Account.validate( :username => "test", :password => "test")
  end
  
  def test_001_new_good_account
    assert_nothing_raised { @new = Account.validate( :username => "test", :password => "test") }

    #make sure it created the file
    assert_nothing_raised do
      File.open(@new.file_name, 'r') do |file|
      end
    end   
   
  end

  def test_002_new_account_bad_values
    assert_raises(RuntimeError) { Account.new( :user => "test", :password => "test") }
    assert_raises(RuntimeError) { Account.new( :username => "test", :pass => "test") }
  
  end
  
  def test_003_has_many_characters_interface
    assert_respond_to(@account, :characters)
    assert_respond_to(@account, :add_character)
    assert_respond_to(@account, :remove_character)
  end
  
  def test_004_has_many_characters_yaml_addition
    assert_respond_to(@account, :to_yaml_properties)
    assert_equal(@account.to_yaml_properties, ['@props', '@characters'])
  end

  def test_005_add_one_character
    assert_nothing_raised {
    @char = with Character.new do |c|
      c.oid = rand(1000)
      c.name = "aberant"
      c.description = "test"
    end
    }
    
    assert_nothing_raised { @account.add_character @char }
    
    assert_not_nil @account.characters
    assert_equal 1, @account.characters.length

  end

  def test_005_add_four_characters
    assert_nothing_raised {
    @char1 = with Character.new do |c|
      c.oid = rand(1000)
      c.name = "aberant1"
      c.description = "test"
    end
    }
    assert_nothing_raised {
    @char2 = with Character.new do |c|
      c.oid = rand(1000)
      c.name = "aberant2"
      c.description = "test"
    end
    }
    assert_nothing_raised {
    @char3 = with Character.new do |c|
      c.oid = rand(1000)
      c.name = "aberant3"
      c.description = "test"
    end
    }
    assert_nothing_raised {
    @char4 = with Character.new do |c|
      c.oid = rand(1000)
      c.name = "aberant4"
      c.description = "test"
    end
    }
    
    assert_nothing_raised { @account.add_character @char1 }
    assert_nothing_raised { @account.add_character @char2 }
    assert_nothing_raised { @account.add_character @char3 }
    assert_raises(RuntimeError) { @account.add_character @char4 }
    
    assert_not_nil @account.characters
    assert_equal 3, @account.characters.length

  end

end
